package com.longge;

/**
 * @创建人 xinglongge
 * @创建时间 2021/10/14
 * @描述
 */
public class IsHappy {
    public boolean isHappy(int n) {
        int fastValue = n, slowValue = n;
        do {
            slowValue = getNext(slowValue);
            fastValue = getNext(fastValue);
            fastValue = getNext(fastValue);
        } while (fastValue != slowValue);
        return fastValue == 1;

    }


    private int getNext(int n) {
        char[] chars = String.valueOf(n).toCharArray();
        int res = 0;
        for (char aChar : chars) {
            int cur = aChar - '0';
            res += Math.pow(cur, 2);
        }
        return res;

    }

    public static void main(String[] args) {
        new IsHappy().isHappy(2);
    }
}
